<?php
/**
 * 检测验证码
 * @param  integer $id 验证码ID
 * @return boolean     检测结果
 */
function check_verify($code, $id = 1){
	ob_clean();
	$verify = new \Think\Verify();
	return $verify->check($code, $id);
}

/**
 * 阿里大鱼短信注册
 * @param  string 	$phone 	手机号
 * @param  string	$tpl 	短信模板
 * @return boolean 			发送成功返回true;失败返回false
 */						
function sendSms($phone='',$tpl='',$code=''){
	Vendor('Alidayu.TopSdk');
    $c = new \TopClient;
    $c->appkey = C('Alidayu.appkey');
    $c->secretKey = C('Alidayu.secretkey');
    $req = new \AlibabaAliqinFcSmsNumSendRequest;
    $req->setExtend("123456");
    $req->setSmsType("normal");
    $req->setSmsFreeSignName("邹金顺");
    $req->setSmsParam("{\"code\":\"$code\"}");
    $req->setRecNum($phone);
    $req->setSmsTemplateCode($tpl);
    $resp = $c->execute($req);
    if($resp->success){
    	return true;
    } else {
    	return false;
    }
}

/**
 * 邮件发送
 * @param  string  $to  	 收件者
 * @param  string  $title    标题
 * @param  string  $content  内容
 * @return boolean			 成功返回 true 失败 返回 false
 */
function sendMail($to, $title, $content) {
    Vendor('PHPMailer.PHPMailerAutoload');
    $mail= new PHPMailer();
    $mail->CharSet = "utf-8"; 
    $mail->IsSMTP();
    $mail->Host = C('Email.smtp');
    $mail->Port = 25;
    $mail->From = C('Email.username');
    $mail->FromName = C('webname');
    $mail->SMTPAuth = true;
    $mail->Username = C('Email.username');
    $mail->Password = C('Email.password');
    $mail->Subject = $title;
    $mail->AltBody = "text/html";
    $mail->Body = $content;
    $mail->IsHTML(true);
    $mail->AddAddress(trim($to));
    if (!$mail->Send()) {
        return false;
    } else {
        return true;
    } 
}

/**
 * 支付宝扫码支付
 * @param array $param 商品参数
 * @return boolean     表单参数
 */
function alipay($param=array()){
    Vendor('Alipay.pagepay.buildermodel.AlipayTradePagePayContentBuilder');
    Vendor('Alipay.pagepay.service.AlipayTradeService');
    $config = C('Alipay');
    $payRequestBuilder = new \AlipayTradePagePayContentBuilder();
    $payRequestBuilder->setBody($param['body']); //描述
    $payRequestBuilder->setSubject($param['subject']); //订单名称
    $payRequestBuilder->setTotalAmount($param['total_amount']); //金额
    $payRequestBuilder->setOutTradeNo($param['out_trade_no']); //订单号
    $aop = new \AlipayTradeService($config);
    $response = $aop->pagePay($payRequestBuilder,$config['return_url'],$config['notify_url']);
    //输出表单
    return $response;
}


/**
 * 微信扫码支付
 * @param  array   $param   参数封装
 * @return string           成功返回 url 失败 返回false
 */
function wxpay($param=array()){
    Vendor('Wxpay.WxPay#Api');
    Vendor('Wxpay.WxPay#NativePay');
    $notify = new \NativePay();
    $input = new \WxPayUnifiedOrder();
    $input->SetBody($param['body']); //描述
    $input->SetAttach($param['attach']); //附加数据
    $input->SetOut_trade_no($param['order_sn']);
    $input->SetTotal_fee($param['fee']); //总金额 单位分
    $input->SetTime_start(date("YmdHis"));
    $input->SetTime_expire(date("YmdHis", time() + 600));
    $input->SetGoods_tag($param['goods_tag']);
    $input->SetNotify_url($param['url']); //回调
    $input->SetTrade_type("NATIVE");
    $input->SetProduct_id($param['product']);
    $result = $notify->GetPayUrl($input);
    $url = $result["code_url"];
    if($url){
        return $url;
    } else {
        return false;
    }
}



 ?>
